package top.longmarch.flow.component;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.longmarch.flow.api.ClassBeanUtil;
import top.longmarch.flow.entity.LiteflowChain;
import top.longmarch.flow.service.ILiteflowChainService;
import top.longmarch.flow.slot.LiteflowChainContext;

import javax.annotation.Resource;

@Slf4j
@Component("flowChainDetailCmp")
public class FlowChainDetailCmp extends NodeComponent {

    @Resource
    private ILiteflowChainService liteflowChainService;

    @Override
    public void process() throws Exception {
        LiteflowChainContext context = this.getContextBean(LiteflowChainContext.class);
        LiteflowChain chain = liteflowChainService.getById(MapUtil.getLong(this.getRequestData(), "id"));
        if (StrUtil.isBlank(chain.getRequestExample()) && StrUtil.isNotEmpty(chain.getRequestBean())) {
            Object o = ClassBeanUtil.getContextBeanClazz(chain.getRequestBean()).newInstance();
            chain.setRequestExample(JSONUtil.toJsonStr(o));
        }
        context.setLiteflowChain(chain);
    }

}
